import Ember from 'ember';
import EmberUploader from 'ember-uploader';
/**
* 我们必须将这个组件中的事件冒泡上去，比如文件上传成功或失败??
* 或者将属性传递给组件？！
*/
var MyUploader = EmberUploader.Uploader.extend({
  ajaxSettings: function(url, params, method) {
    var settings =  this._super(url, params, method);

    settings.headers = {
      "X-bucketname": "api_bucket",
      "Authorization": "Basic YXBpX3VzZXJuYW1lOmFwaV9wYXNzd29yZA=="
    };
    return settings;
  }
});

export default EmberUploader.FileField.extend({
  url: '',
  filesDidChange: function(files) {
    var uploadUrl = this.get('url'),
        self = this,
        uploader;

    this.set('uploading', true);

    uploader = MyUploader.create({
      url: uploadUrl
    });

    if (!Ember.isEmpty(files)) {
      uploader.upload(files[0]).then(function(data){
        console.log(data);
        self.set('uploading', false);
        self.set('uploadResponse', data);
      }, function(err){
        console.log(err);
        self.set('uploading', false);
        self.set('uploadResponse', err);
      });
    }
  }
});
